//
//  YWKLoginViewController.m
//  YWKUser
//
//  Created by renyi on 3/23/16.
//  Copyright © 2016 iCareTech. All rights reserved.
//

#import "YWKLoginViewController.h"
#import "YWKConfiguration.h"
#import "UIColor+ICT.h"
#import "Masonry.h"
#import "RMUniversalAlert.h"
#import "YWKRegisterViewController.h"
#import "FiexibleTool.h"
#import "YWKVerificationCodeViewController.h"
#import "YWKUser.h"
#import "YWKLoginHander.h"
#import "YWKConfiguration.h"
#import "YWKHelper.h"
#import "YWKLable.h"
#import "MBProgressHUD+YWK.h"
#import <RMUniversalAlert.h>
#import "NSString+YWKChange.h"
#import <RealReachability.h>
#import <DDLog.h>

#ifdef DEBUG
static const int ddLogLevel = LOG_LEVEL_INFO;
#else
static const int ddLogLevel = LOG_LEVEL_ERROR;
#endif

#define kCurrentTF 100
NSString * const YWKShowLoginNotificateion = @"YWKShowLoginNotificateion";


@interface YWKLoginViewController ()<
UITextFieldDelegate,
YWKLoginHander,
UIAlertViewDelegate>


@property (strong, nonatomic) UIView *loginContainerView;

@property (strong, nonatomic) UIButton * registerBtn;
@property (strong, nonatomic) UIButton * backBtn;

@property (strong, nonatomic) UIImageView * iconImageView;

@property (strong, nonatomic) UIView * appAndClientContainer;
@property (strong, nonatomic) UIView * placeLineView;
@property (strong, nonatomic) YWKLable * appNameLab;
@property (strong, nonatomic) YWKLable * clientNameLab;

@property (strong, nonatomic) YWKLable * loginCommentLab;

@property (strong, nonatomic) UIView * accountAndPasswordContainer;
@property (strong, nonatomic) UIView * accountAndPasswordSeparate;
@property (strong, nonatomic) YWKLable * accountLab;
@property (strong, nonatomic) YWKLable * passwordLab;
@property (strong, nonatomic) UITextField *accountTF;
@property (strong, nonatomic) UITextField *passwordTF;

@property (strong, nonatomic) UIButton * loginBtn;
@property (strong, nonatomic) UIButton * findPasswordBtn;
@property (strong, nonatomic) UIButton * codeLoginBtn;

@property (nonatomic, strong) YWKLoginHander *loginHander;
//@property (nonatomic, strong) UIAlertView * alertView;
@property (nonatomic, strong) MBProgressHUD *HUD;
//@property (nonatomic, strong) RMUniversalAlert *alertView;
//如果需要在VC加载出来时显示提示信息
@property (nonatomic, copy) NSString *promptWhenDisplay;

@end

@implementation YWKLoginViewController

#pragma mark - life cycle

- (instancetype)initWithPrompt:(NSString *)prompt;
{
    if (self = [super init])
    {
        _promptWhenDisplay = prompt;
    }
    
    return self;
}

- (void)loadView
{
    [super loadView];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.loginContainerView];
    [self.loginContainerView addSubview:self.iconImageView];
    [self.loginContainerView addSubview:self.loginCommentLab];
    [self.loginContainerView addSubview:self.appAndClientContainer];
    [self.loginContainerView addSubview:self.accountAndPasswordContainer];
    [self.loginContainerView addSubview:self.loginBtn];
    [self.loginContainerView addSubview:self.registerBtn];
    [self.loginContainerView addSubview:self.backBtn];
    [self.loginContainerView addSubview:self.findPasswordBtn];
    [self.loginContainerView addSubview:self.codeLoginBtn];
    UINavigationBar * bar = [UINavigationBar appearance];
    [bar setBarStyle:UIBarStyleDefault];
    [bar setBackgroundColor:[UIColor whiteColor]];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.loginContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view).mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    // icon
    CGFloat iconWidth = [FiexibleTool floatWithOriginalFloat:135 isFlexibleHeight:YES];
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.loginContainerView).offset([FiexibleTool
                                                          floatWithOriginalFloat:80
                                                          isFlexibleHeight:YES]);
        make.centerX.equalTo(self.loginContainerView);
        make.size.mas_equalTo(CGSizeMake(iconWidth, iconWidth));
    }];
    
    // 注册
    [self.registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.loginContainerView).mas_equalTo(@([FiexibleTool
                                                               floatWithOriginalFloat:23.f
                                                               isFlexibleHeight:YES]));
        make.trailing.equalTo(self.loginContainerView).mas_equalTo(@([FiexibleTool
                                                                      floatWithOriginalFloat:-16.f
                                                                      isFlexibleHeight:YES]));
        make.size.mas_equalTo(CGSizeMake(60, 30));
    }];
    
    // 返回
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.loginContainerView).mas_equalTo(@([FiexibleTool
                                                               floatWithOriginalFloat:23.f
                                                               isFlexibleHeight:YES]));
        make.left.equalTo(self.loginContainerView).mas_equalTo(@([FiexibleTool
                                                                      floatWithOriginalFloat:11.f
                                                                      isFlexibleHeight:YES]));
        make.size.mas_equalTo(CGSizeMake([FiexibleTool floatWithOriginalFloat:15.f
                                                             isFlexibleHeight:YES],
                                         [FiexibleTool floatWithOriginalFloat:26
                                                             isFlexibleHeight:YES]));
    }];
    
    [self.appAndClientContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.iconImageView.mas_bottom).mas_equalTo(@([FiexibleTool
                                                                       floatWithOriginalFloat:23.
                                                                       isFlexibleHeight:YES]));
        make.leading.equalTo(self.loginContainerView).mas_equalTo(@0);
        make.trailing.equalTo(self.loginContainerView).mas_equalTo(@0);
        make.height.mas_equalTo(@40);
    }];
    
    [self.placeLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.appAndClientContainer.mas_right).mas_equalTo(@([FiexibleTool floatWithOriginalFloat:-140 isFlexibleHeight:YES]));
        make.top.equalTo(self.appAndClientContainer);
        make.size.mas_equalTo(CGSizeMake(1, 40));
    }];
    
    // 云卫康
    [self.appNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.appAndClientContainer);
        make.height.mas_equalTo(@([FiexibleTool
                                   floatWithOriginalFloat:40 isFlexibleHeight:YES]));
        make.centerX.equalTo(self.appAndClientContainer).offset(-[FiexibleTool floatWithOriginalFloat:30 isFlexibleHeight:YES]);
        make.width.mas_equalTo([FiexibleTool floatWithOriginalFloat:165.f isFlexibleHeight:YES]/2.f);
    }];
    
    // 用户端
    [self.clientNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.appNameLab);
        make.left.equalTo(self.appNameLab.mas_right).offset(5);
        make.size.mas_equalTo(CGSizeMake([FiexibleTool floatWithOriginalFloat:51 isFlexibleHeight:YES],
                                         [FiexibleTool floatWithOriginalFloat:20 isFlexibleHeight:YES]));
    }];
    
    //登录您的云卫康帐号
    [self.loginCommentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.appAndClientContainer.mas_bottom).mas_equalTo(@([FiexibleTool floatWithOriginalFloat:5 isFlexibleHeight:YES]));
        make.leading.equalTo(self.loginContainerView).mas_equalTo(@0);
        make.trailing.equalTo(self.loginContainerView).mas_equalTo(@0);
        make.height.mas_equalTo(@([FiexibleTool floatWithOriginalFloat:30
                                                      isFlexibleHeight:YES]));
    }];
    
    // 账号密码相关
    [self.accountAndPasswordContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.loginCommentLab.mas_bottom).mas_equalTo(@([FiexibleTool floatWithOriginalFloat:10 isFlexibleHeight:YES]));
        make.leading.equalTo(self.loginContainerView).mas_equalTo(@14);
        make.trailing.equalTo(self.loginContainerView).mas_equalTo(@(-14));
        make.height.mas_equalTo(@([FiexibleTool floatWithOriginalFloat:87
                                                      isFlexibleHeight:YES]));
    }];
    // 账号
    [self.accountLab  mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.accountAndPasswordContainer);
        make.leading.equalTo(self.accountAndPasswordContainer).mas_equalTo(@([FiexibleTool
                                                                              floatWithOriginalFloat:21
                                                                              isFlexibleHeight:YES]));
        make.width.mas_equalTo(@40);
        make.height.mas_equalTo(@([FiexibleTool floatWithOriginalFloat:42
                                                      isFlexibleHeight:YES]));
    }];
    // 密码
    [self.passwordLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.accountAndPasswordContainer).mas_equalTo(@([FiexibleTool
                                                                          floatWithOriginalFloat:43
                                                                          isFlexibleHeight:YES]));
        make.leading.equalTo(self.accountAndPasswordContainer).mas_equalTo(@([FiexibleTool
                                                                              floatWithOriginalFloat:21
                                                                              isFlexibleHeight:YES]));
        make.width.mas_equalTo(@40);
        make.height.mas_equalTo(@([FiexibleTool floatWithOriginalFloat:42
                                                      isFlexibleHeight:YES]));
    }];
    
    // 账号TF
    [self.accountTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.accountAndPasswordContainer);
        make.leading.equalTo(self.accountAndPasswordContainer).mas_equalTo(@(15 + 40 + 20));
        make.trailing.equalTo(self.accountAndPasswordContainer).mas_equalTo(@0);
        make.height.mas_equalTo(@([FiexibleTool floatWithOriginalFloat:42
                                                      isFlexibleHeight:YES]));
    }];
    
    
    // 密码TF
    [self.passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.accountAndPasswordContainer).mas_equalTo(@([FiexibleTool
                                                                          floatWithOriginalFloat:43
                                                                          isFlexibleHeight:YES]));
        make.leading.equalTo(self.accountAndPasswordContainer).mas_equalTo(@(15 + 40 + 20));
        make.trailing.equalTo(self.accountAndPasswordContainer).mas_equalTo(@0);
        make.height.mas_equalTo(@([FiexibleTool floatWithOriginalFloat:42
                                                      isFlexibleHeight:YES]));
    }];
    
    [self.accountAndPasswordSeparate mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.accountAndPasswordContainer).mas_equalTo(@([FiexibleTool floatWithOriginalFloat:42 isFlexibleHeight:YES]));
        make.leading.equalTo(self.accountAndPasswordContainer);
        make.trailing.equalTo(self.accountAndPasswordContainer);
        make.height.mas_equalTo(@1);
    }];
    
    // 立即登录
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.accountAndPasswordContainer.mas_bottom).mas_equalTo(@([FiexibleTool
                                                                                     floatWithOriginalFloat:16
                                                                                     isFlexibleHeight:YES]));
        make.leading.equalTo(self.loginContainerView).mas_equalTo(@14);
        make.trailing.equalTo(self.loginContainerView).mas_equalTo(@(-14));
        make.height.mas_equalTo(@([FiexibleTool floatWithOriginalFloat:40
                                                      isFlexibleHeight:YES]));
    }];
    
    // 找回密码
    [self.findPasswordBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.loginContainerView).mas_equalTo(@([FiexibleTool
                                                                    floatWithOriginalFloat:-22
                                                                    isFlexibleHeight:YES]));
        make.leading.equalTo(self.loginContainerView).mas_equalTo(@([FiexibleTool
                                                                     floatWithOriginalFloat:17
                                                                     isFlexibleHeight:YES]));
        make.size.mas_equalTo(CGSizeMake(100, 30));
    }];
    // 验证码登录
    [self.codeLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.findPasswordBtn);
        make.trailing.equalTo(self.loginContainerView).mas_equalTo(@([FiexibleTool
                                                                      floatWithOriginalFloat:-16
                                                                      isFlexibleHeight:YES]));
        make.size.mas_equalTo(CGSizeMake(150, 30));
    }];
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillShowNotification
                                                  object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillHideNotification
                                                  object:nil];
    // Do any additional setup after loading the view.
    
//    self.view.backgroundColor = [UIColor whiteColor];
//    
//    self.navigationController.navigationBarHidden = YES;
//    
//    [self.view addSubview:self.registerBtn];
//    
//    [self.registerBtn mas_makeConstraints:^(MASConstraintMaker *make)
//    {
//        make.top.equalTo(self.view.mas_top).with.offset(37.f);
//        make.right.equalTo(self.view.mas_right).with.offset(- 16.f);
//        make.width.equalTo(@40);
//    }];

}

//- (void)viewDidAppear:(BOOL)animated
//{
//    [super viewDidAppear:animated];
//    
//    if (self.promptWhenDisplay.length > 0)
//    {
//        [RMUniversalAlert showAlertInViewController:self
//                                          withTitle:NSLocalizedString(@"提示", nil)
//                                            message:self.promptWhenDisplay
//                                  cancelButtonTitle:NSLocalizedString(@"确定", nil)
//                             destructiveButtonTitle:nil
//                                  otherButtonTitles:nil
//                                           tapBlock:^(RMUniversalAlert * _Nonnull alert, NSInteger buttonIndex) {
//                                               self.promptWhenDisplay = nil;
//                                           }];
//    }
//
//}
- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
}

//- (void)popVC{
//    
//    [self.navigationController popViewControllerAnimated:YES];
//}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
                                                    replacementString:(NSString *)string
{
    if ([string isEqualToString:@"\n"]) {
        [textField resignFirstResponder];
        return NO;
    }
    return YES;
}



#pragma mark - notifation
- (void)keyboardWillShow:(NSNotification *)keyboardWillShowNotifi
{
//    NSDictionary * keyboardInfo = keyboardWillShowNotifi.userInfo;
//    CGSize keyboardSize = [[keyboardInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
//    CGFloat duration = [[keyboardInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
//    
//    // 修改约束 ...
//    [self.iconImageView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.loginContainerView).mas_equalTo(@(-180));
//        make.centerX.equalTo(self.loginContainerView).mas_equalTo(@0);
//        make.size.mas_equalTo(CGSizeMake(135, 135));
//    }];
//    [self.appAndClientContainer mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.iconImageView.mas_bottom).mas_equalTo(@(80));
//        make.leading.equalTo(self.loginContainerView).mas_equalTo(@0);
//        make.trailing.equalTo(self.loginContainerView).mas_equalTo(@0);
//        make.height.mas_equalTo(@40);
//    }];
//    [self.findPasswordBtn mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(self.loginContainerView).mas_equalTo(@(-(keyboardSize.height - 30 + 10 + 30)));
//        make.leading.equalTo(self.loginContainerView).mas_equalTo(@20);
//        make.size.mas_equalTo(CGSizeMake(100, 30));
//    }];
//    [self.loginContainerView setNeedsUpdateConstraints];
//    [UIView animateWithDuration:duration animations:^{
//        [self.loginContainerView layoutIfNeeded];
//    }];
}

- (void)keyboardWillHide:(NSNotification *)keyboardWillHideNotifi
{
//    NSDictionary * keyboardInfo = keyboardWillHideNotifi.userInfo;
//    CGFloat duration = [[keyboardInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
//    
//    // 修改约束 ...
//    [self.iconImageView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.loginContainerView).mas_equalTo(@(60));
//        make.centerX.equalTo(self.loginContainerView).mas_equalTo(@0);
//        make.size.mas_equalTo(CGSizeMake(135, 135));
//    }];
//    [self.appAndClientContainer mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.iconImageView.mas_bottom).mas_equalTo(@(30 ));
//        make.leading.equalTo(self.loginContainerView).mas_equalTo(@0);
//        make.trailing.equalTo(self.loginContainerView).mas_equalTo(@0);
//        make.height.mas_equalTo(@40);
//    }];
//    [self.findPasswordBtn mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(self.loginContainerView).mas_equalTo(@(-30));
//        make.leading.equalTo(self.loginContainerView).mas_equalTo(@20);
//        make.size.mas_equalTo(CGSizeMake(100, 30));
//    }];
//
//    [self.loginContainerView setNeedsUpdateConstraints];
//    [UIView animateWithDuration:duration animations:^{
//        [self.loginContainerView layoutIfNeeded];
//    }];
}

#pragma mark - btnclick
/**
 *  注册
 *
 *  @param sender 
 */
- (void)registerBtnClick:(UIButton *)sender {
    
    RealReachability * real = [RealReachability sharedInstance];
    if(real.currentReachabilityStatus == RealStatusUnknown ||
       real.currentReachabilityStatus == RealStatusNotReachable)
    {
        [MBProgressHUD hubWithTitle:NSLocalizedString(@"网络不给力，稍后再试试",nil)
                             second:3
                             toView:self.view];
        return;
    }
    
    YWKRegisterViewController * registerVC = [[YWKRegisterViewController alloc] init];
    registerVC.pwdType = YWKFromLoginRegistVC;
    [self.navigationController pushViewController:registerVC animated:YES];
}

/**
 *  立即登录
 *
 *  @param sender
 */
- (void)loginClick:(UIButton *)sender
{
    [self validateLoginInfo];
}


/**
 *  忘记密码
 *
 *  @param sender
 */
- (void)forgotPasswordClick:(UIButton *)sender {
    
    RealReachability * real = [RealReachability sharedInstance];
    if(real.currentReachabilityStatus == RealStatusUnknown ||
       real.currentReachabilityStatus == RealStatusNotReachable)
    {
        [MBProgressHUD hubWithTitle:NSLocalizedString(@"网络不给力，稍后再试试",nil)
                             second:3
                             toView:self.view];
        return;
    }
    YWKRegisterViewController * registerVC = [[YWKRegisterViewController alloc] init];
    registerVC.pwdType = YWKFromLoginFindBackVC;
    [self.navigationController pushViewController:registerVC animated:YES ];
}

/**
 *  使用验证码登录
 *
 */
- (void)verificationCodeClick:(UIButton *)sender {
    
    RealReachability * real = [RealReachability sharedInstance];
    if(real.currentReachabilityStatus == RealStatusUnknown ||
       real.currentReachabilityStatus == RealStatusNotReachable)
    {
        [MBProgressHUD hubWithTitle:NSLocalizedString(@"网络不给力，稍后再试试",nil)
                             second:3
                             toView:self.view];
        return;
    }
    YWKVerificationCodeViewController * verificationCodeLoginVC = [[YWKVerificationCodeViewController alloc] init];
    [self.navigationController pushViewController:verificationCodeLoginVC animated:YES];
}

/**
 *  返回
 */

- (void)backBtnClick{
   
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark - YWKLoginHanderDelegate

- (void)loginSuccessForAccount:(YWKUser *)account
{
    self.HUD.mode = MBProgressHUDModeCustomView;
    self.HUD.labelText = @"登录成功!";
    UIImage *image = [UIImage imageNamed:@"checkout_white"];
    UIImageView * imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    imageview.image = image;
    self.HUD.customView = imageview;
    self.HUD.labelFont = [UIFont fontWithName:kTextFontName
                                         size:[FiexibleTool floatWithOriginalFloat:14.f
                                                                  isFlexibleHeight:YES]];
    DDLogInfo(@"account.userName = %@,account.loginName = %@, account.displayName = %@, account.password = %@",
          account.userName, account.loginName, account.displayName, account.password);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.HUD removeFromSuperview];
        [self dismissViewControllerAnimated:YES completion:nil];
    });
}

- (void)loginFailedForAccount:(YWKUser *)account withError:(NSError *)error
{
    DDLogInfo(@"loginFailedForAccount error = %@", [error localizedDescription]);
    self.HUD.mode = MBProgressHUDModeText;
    self.HUD.labelText = error.userInfo[@"description"];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    
       [self.HUD removeFromSuperview];
    });
}


#pragma mark - private methods

/**
 *  校验登录信息
 *
 *  @return
 */
- (void)validateLoginInfo
{
    if ([self.accountTF.text isEqualToString: @""]) {
        
        self.accountTF.tag = kCurrentTF;
        [self showIdentifCodeWithString:NSLocalizedString(@"请输入手机号/帐号!", nil)];
    }
    else if (![YWKHelper isValidateUsername:[self.accountTF.text removeHeadAndFootSpace]]) {
        
        self.accountTF.tag = kCurrentTF;
        [self showIdentifCodeWithString:NSLocalizedString(@"请输入正确的手机号/帐号！", nil)];
    }
    else if ([self.passwordTF.text  isEqualToString: @""] ||
             ![YWKHelper isValidatePassword:self.passwordTF.text]) {
        
        self.passwordTF.tag = kCurrentTF;
        self.accountTF.tag = kCurrentTF + 1;
        [self showIdentifCodeWithString:NSLocalizedString(@"请输入6-15位数字或字母!", nil)];
    }
    else
    {
        self.HUD = [[MBProgressHUD alloc]initWithView:self.view];
        self.HUD.activityIndicatorColor = kYWKButtonBlueColor;
        self.HUD.labelText = @"正在登录";
        [self.view addSubview:self.HUD];
        [self.HUD show:YES];

        NSString *loginName = [self.accountTF.text removeHeadAndFootSpace];
        //登录
        YWKUser *loginUser = [[YWKUser alloc] initWithLoginName:loginName];
        self.loginHander = [YWKLoginHander loginHandler];
        [self.loginHander loginHandlerForAccount:loginUser withDelegate:self error:nil];
        [self.loginHander startLoginWithPassword:self.passwordTF.text withError:nil];
    }
    
}

-(void)clearUserAccont{
    
    self.accountTF.text = nil;
    self.passwordTF.text = nil;
}

- (void)showIdentifCodeWithString:(NSString *)alertString
{
    [MBProgressHUD hubWithTitle:alertString second:3 toView:self.view];
}


#pragma mark - getters and setters

- (UIView *)loginContainerView
{
    if (!_loginContainerView) {
        
        _loginContainerView = [[UIView alloc] initWithFrame:CGRectZero];
        _loginContainerView.backgroundColor = [UIColor clearColor];
        _loginContainerView.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _loginContainerView;
}

- (UIButton *)registerBtn
{
    if (!_registerBtn) {
        _registerBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _registerBtn.translatesAutoresizingMaskIntoConstraints = NO;
        [_registerBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
        _registerBtn.titleLabel.font = [UIFont systemFontOfSize:[FiexibleTool
                                                                 floatWithOriginalFloat:17.f
                                                                 isFlexibleHeight:YES]];
        [_registerBtn setTitle:NSLocalizedString(@"注册", nil) forState:UIControlStateNormal];
        [_registerBtn setTitleColor:[UIColor colorWithHex:0x009FE9] forState:UIControlStateNormal];
        [_registerBtn addTarget:self
                         action:@selector(registerBtnClick:)
               forControlEvents:UIControlEventTouchUpInside];
    }
    return _registerBtn;
}

-(UIButton *)backBtn{
    
    if (!_backBtn) {
        
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _backBtn.translatesAutoresizingMaskIntoConstraints = NO;
        [_backBtn setImage:[UIImage imageNamed:@"nav_backArrow"]
                  forState:UIControlStateNormal];
        [_backBtn addTarget:self
                     action:@selector(backBtnClick)
           forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

- (UIImageView *)iconImageView
{
    if (!_iconImageView) {
        
        _iconImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        _iconImageView.layer.cornerRadius = [FiexibleTool floatWithOriginalFloat:30.f
                                                                isFlexibleHeight:YES];
        _iconImageView.backgroundColor = [UIColor clearColor];
        _iconImageView.image = [UIImage imageNamed:@"LOGO_Login"];
        _iconImageView.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _iconImageView;
}

- (UIView *)appAndClientContainer
{
    if (!_appAndClientContainer) {
        
        _appAndClientContainer = [[UIView alloc] initWithFrame:CGRectZero];
        _appAndClientContainer.translatesAutoresizingMaskIntoConstraints = NO;
        _appAndClientContainer.backgroundColor = [UIColor clearColor];
        
        [_appAndClientContainer addSubview:self.placeLineView];
        [_appAndClientContainer addSubview:self.appNameLab];
        [_appAndClientContainer addSubview:self.clientNameLab];
    }
    return _appAndClientContainer;
}

- (UIView *)placeLineView
{
    if (!_placeLineView) {
        
        _placeLineView = [[UIView alloc] initWithFrame:CGRectZero];
        _placeLineView.translatesAutoresizingMaskIntoConstraints = NO;
        _placeLineView.backgroundColor = [UIColor clearColor];
    }
    return _placeLineView;
}

- (YWKLable *)appNameLab
{
    if (!_appNameLab) {
        
        _appNameLab = [[YWKLable alloc] initWithFrame:CGRectZero];
        _appNameLab.translatesAutoresizingMaskIntoConstraints = NO;
        _appNameLab.font = [UIFont systemFontOfSize:[FiexibleTool
                                                     floatWithOriginalFloat:26.
                                                     isFlexibleHeight:YES]];
        _appNameLab.textAlignment = NSTextAlignmentRight;
        _appNameLab.text = NSLocalizedString(@"云卫康", nil);
    }
    return _appNameLab;
}

- (YWKLable *)clientNameLab
{
    if (!_clientNameLab) {
        
        _clientNameLab = [[YWKLable alloc] initWithFrame:CGRectZero];
        _clientNameLab.translatesAutoresizingMaskIntoConstraints = NO;
        _clientNameLab.backgroundColor = [UIColor colorWithHex:0x009FE9];
        _clientNameLab.font = [UIFont systemFontOfSize:[FiexibleTool
                                                        floatWithOriginalFloat:12.f
                                                        isFlexibleHeight:YES]];
        _clientNameLab.layer.cornerRadius = 6.f;
        _clientNameLab.layer.masksToBounds = YES;
        _clientNameLab.textAlignment = NSTextAlignmentCenter;
        _clientNameLab.textColor = [UIColor whiteColor];
        _clientNameLab.text = NSLocalizedString(@"用户端", nil);
    }
    return _clientNameLab;
}

- (YWKLable *)loginCommentLab
{
    if (!_loginCommentLab) {
        
        _loginCommentLab = [[YWKLable alloc] initWithFrame:CGRectZero];
        _loginCommentLab.translatesAutoresizingMaskIntoConstraints = NO;
        _loginCommentLab.textAlignment = NSTextAlignmentCenter;
        _loginCommentLab.font = [UIFont systemFontOfSize:14.f];
        _loginCommentLab.textColor = [UIColor lightGrayColor];
        _loginCommentLab.text = NSLocalizedString(@"登录您的云卫康帐号", nil);
    }
    return _loginCommentLab;
}

- (UIView *)accountAndPasswordContainer
{
    if (!_accountAndPasswordContainer) {
        
        _accountAndPasswordContainer = [[UIView alloc] initWithFrame:CGRectZero];
        _accountAndPasswordContainer.layer.cornerRadius = 10;
        _accountAndPasswordContainer.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _accountAndPasswordContainer.layer.borderWidth = 1;
        _accountAndPasswordContainer.layer.masksToBounds = YES;
        _accountAndPasswordContainer.translatesAutoresizingMaskIntoConstraints = NO;
        
        [_accountAndPasswordContainer addSubview:self.accountAndPasswordSeparate];
        [_accountAndPasswordContainer addSubview:self.accountLab];
        [_accountAndPasswordContainer addSubview:self.passwordLab];
        [_accountAndPasswordContainer addSubview:self.accountTF];
        [_accountAndPasswordContainer addSubview:self.passwordTF];
    }
    return _accountAndPasswordContainer;
}

- (UIView *)accountAndPasswordSeparate
{
    if (!_accountAndPasswordSeparate) {
        
        _accountAndPasswordSeparate = [[UIView alloc] initWithFrame:CGRectZero];
        _accountAndPasswordSeparate.translatesAutoresizingMaskIntoConstraints = NO;
        _accountAndPasswordSeparate.backgroundColor = [UIColor lightGrayColor];
    }
    return _accountAndPasswordSeparate;
}

- (YWKLable *)accountLab
{
    if (!_accountLab) {
        
        _accountLab = [[YWKLable alloc] initWithFrame:CGRectZero];
        _accountLab.translatesAutoresizingMaskIntoConstraints = NO;
        _accountLab.font = [UIFont boldSystemFontOfSize:14.f];
        _accountLab.text = NSLocalizedString(@"帐号", nil);
    }
    return _accountLab;
}

- (YWKLable *)passwordLab
{
    if (!_passwordLab) {
        
        _passwordLab = [[YWKLable alloc] initWithFrame:CGRectZero];
        _passwordLab.translatesAutoresizingMaskIntoConstraints = NO;
        _passwordLab.font = [UIFont boldSystemFontOfSize:14.f];
        _passwordLab.text = NSLocalizedString(@"密码", nil);
    }
    return _passwordLab;
}

- (UITextField *)accountTF
{
    if (!_accountTF) {
        
        _accountTF = [[UITextField alloc] initWithFrame:CGRectZero];
        _accountTF.font = [UIFont systemFontOfSize:14];
        _accountTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _accountTF.translatesAutoresizingMaskIntoConstraints = NO;
        _accountTF.delegate = self;
        _accountTF.placeholder = NSLocalizedString(@"请输入手机号/帐号", nil);
    }
    return _accountTF;
}

- (UITextField *)passwordTF
{
    if (!_passwordTF) {
        
        _passwordTF = [[UITextField alloc] initWithFrame:CGRectZero];
        _passwordTF.font = [UIFont systemFontOfSize:14];
        _passwordTF.translatesAutoresizingMaskIntoConstraints = NO;
        _passwordTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _passwordTF.secureTextEntry = YES;
        _passwordTF.autocorrectionType = UITextAutocorrectionTypeNo;
        _passwordTF.autocapitalizationType = UITextAutocapitalizationTypeNone;
        _passwordTF.delegate = self;
        _passwordTF.clearsOnBeginEditing = YES;
        _passwordTF.placeholder = NSLocalizedString(@"请输入密码", nil);
    }
    return _passwordTF;
}

- (UIButton *)loginBtn
{
    if (!_loginBtn) {
        
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _loginBtn.translatesAutoresizingMaskIntoConstraints = NO;
        _loginBtn.titleLabel.font = [UIFont systemFontOfSize:[FiexibleTool floatWithOriginalFloat:20
                                                                                 isFlexibleHeight:YES]];
        _loginBtn.layer.cornerRadius = [FiexibleTool floatWithOriginalFloat:17 isFlexibleHeight:YES];
        _loginBtn.layer.borderColor = [UIColor colorWithHex:0x009FE9].CGColor;
        _loginBtn.layer.borderWidth = 1.0;
        _loginBtn.layer.masksToBounds = YES;
        [_loginBtn setTitleColor:[UIColor colorWithHex:0x009FE9] forState:UIControlStateNormal];
        [_loginBtn setTitle:NSLocalizedString(@"立即登录", nil) forState:UIControlStateNormal];
        [_loginBtn addTarget:self
                      action:@selector(loginClick:)
            forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginBtn;
}

- (UIButton *)findPasswordBtn
{
    if (!_findPasswordBtn) {
        
        _findPasswordBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _findPasswordBtn.translatesAutoresizingMaskIntoConstraints = NO;
        _findPasswordBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_findPasswordBtn setTitleColor:[UIColor colorWithHex:0x009FE9] forState:UIControlStateNormal];
        [_findPasswordBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [_findPasswordBtn setTitle:NSLocalizedString(@"找回密码", nil) forState:UIControlStateNormal];
        [_findPasswordBtn addTarget:self
                             action:@selector(forgotPasswordClick:)
                   forControlEvents:UIControlEventTouchUpInside];
    }
    return _findPasswordBtn;
}

- (UIButton *)codeLoginBtn
{
    if (!_codeLoginBtn) {
        
        _codeLoginBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _codeLoginBtn.translatesAutoresizingMaskIntoConstraints = NO;
        _codeLoginBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_codeLoginBtn setTitleColor:[UIColor colorWithHex:0x009FE9] forState:UIControlStateNormal];
        [_codeLoginBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
        [_codeLoginBtn setTitle:NSLocalizedString(@"使用验证码登录", nil) forState:UIControlStateNormal];
        [_codeLoginBtn addTarget:self
                          action:@selector(verificationCodeClick:)
                forControlEvents:UIControlEventTouchUpInside];
    }
    return _codeLoginBtn;
}

@end

